/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GamePlay;

import FrameWork.Audio.RSAudio;
import FrameWork.IPlay;
import FrameWork.IState;
import FrameWork.Level;
import FrameWork.Point;
import FrameWork.RSFont;
import FrameWork.Rectangle;
import java.io.IOException;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author DANTRUONG
 */
public class State_Level extends IState{
    int[] m_Lock = new int[Level.get().getTotalLevel()];
    int m_IDChoosed;
    int m_Mask;
    int m_Level;
    int m_Status;
    int m_IDChoosed1;
    int m_FPS;
    
    Sprite m_Home;
    Sprite m_Ball;
    Sprite m_Lock1;
    Sprite m_Point;
    Point [] m_CurPos = new Point[Level.get().getTotalLevel()];
    Rectangle[] m_Rect = new Rectangle[Level.get().getTotalLevel()];
    Rectangle m_Screen;
    Rectangle[] m_RectButton = new Rectangle[3];
    Point m_NextPos;
    
    public State_Level(IPlay _Play){
        super(_Play);
        m_IDState = Global.STATE_LEVEL;
    }
    
    public void Init (){
        super.Init();
        m_Status = 0;
        try {
            m_Home = new Sprite(Image.createImage("/Image/home.png"),
                        42, 42);           
            m_Point = new Sprite(Image.createImage("/Image/next.png"),
                        36,48); 
            m_Ball = new Sprite (Image.createImage("/Image/ball.png"),
                        60, 60);    
            m_Lock1 = new Sprite (Image.createImage("/Image/lock.png"),
                        40, 40);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
         m_Level = Level.get().getTotalLevel();
        for(int i=0; i<m_Level; i++){
            m_CurPos[i] = new Point(20 + (i%3)*70, 25 + (i/3)*70);
            m_Rect[i] = new Rectangle(m_CurPos[i].X, m_CurPos[i].Y, 60, 60);
        }
        for(int i=0; i<3; i++)
            m_RectButton[i] = new Rectangle(20 + 70*i, 235, 60, 60);
        m_NextPos = m_CurPos[0];
        m_IDChoosed = 0;
        m_IDChoosed1 = -1;
        m_Mask = 0;
        m_FPS = 0;
        for(int i=1; i<=Level.get().getUnlockLevel(); i++)
            m_Lock[i-1] = 1;
        for(int i=21; i<Level.get().getUnlockLevel(); i++)
            m_Lock[i] = 0;
        m_Screen = new Rectangle(20, 25, 200, 200);
    }
    
    public void Update (float _Time){
        switch(m_Status){
            case 1:
                if(m_CurPos[0].Y <m_NextPos.Y){
                    for(int i=0; i<m_Level; i++){
                        m_CurPos[i] = new Point(20 + (i%3)*70 ,m_CurPos[i].Y + 7);
                        m_Rect[i] = new Rectangle(m_CurPos[i].X, m_CurPos[i].Y, 60, 60);
                    }
                }
                if(m_CurPos[0].Y >m_NextPos.Y){
                    for(int i=0; i<m_Level; i++){
                        m_CurPos[i] = new Point(20 + (i%3)*70 ,m_CurPos[i].Y - (m_CurPos[0].Y - m_NextPos.Y));
                        m_Rect[i] = new Rectangle(m_CurPos[i].X, m_CurPos[i].Y, 60, 60);
                    }
                }
                if(m_CurPos[0].Y == m_NextPos.Y){
                    m_Status = 0;
                }
                break;
            case 2:
                if(m_CurPos[0].Y >m_NextPos.Y){
                    for(int i=0; i<m_Level; i++){
                        m_CurPos[i] = new Point(20 + (i%3)*70 ,m_CurPos[i].Y - 7);
                        m_Rect[i] = new Rectangle(m_CurPos[i].X, m_CurPos[i].Y, 60, 60);
                    }
                }
                if(m_CurPos[0].Y <m_NextPos.Y){
                    for(int i=0; i<m_Level; i++){
                        m_CurPos[i] = new Point(20 + (i%3)*70 ,m_CurPos[i].Y + (m_NextPos.Y - m_CurPos[0].Y));
                        m_Rect[i] = new Rectangle(m_CurPos[i].X, m_CurPos[i].Y, 60, 60);
                    }
                }
                if(m_CurPos[0].Y == m_NextPos.Y){
                    m_Status = 0;
                }
                break;
        }
    }
    public void PointerPressed(int x, int y){
        if(m_Status == 0){
            for(int i = 0; i<Level.get().getTotalLevel(); i++){
                        if(m_Rect[i].Intersects(new Point(x,y)) && m_Screen.Intersects(new Point(x, y))){
                            m_IDChoosed = i;
                            if(m_CurPos[i].Y == 25){
                                if(m_CurPos[i].X == 20)
                                    m_Mask = 0;
                                if(m_CurPos[i].X == 90)
                                    m_Mask = 1;
                                if(m_CurPos[i].X == 160)
                                    m_Mask = 2;
                            }
                            if(m_CurPos[i].Y == 95){
                                if(m_CurPos[i].X == 20)
                                    m_Mask = 3;
                                if(m_CurPos[i].X == 90)
                                    m_Mask = 4;
                                if(m_CurPos[i].X == 160)
                                    m_Mask = 5;
                            }
                            if(m_CurPos[i].Y == 165){
                                if(m_CurPos[i].X == 20)
                                    m_Mask = 6;
                                if(m_CurPos[i].X == 90)
                                    m_Mask = 7;
                                if(m_CurPos[i].X == 160)
                                    m_Mask = 8;
                            }
                            RSAudio.getRS().PlayExplosion();
                            if(m_Lock[m_IDChoosed]==1){
                                RSAudio.getRS().StopBack();
                                Level.get().setCurLevel(m_IDChoosed + 1);
                                m_Play.setNextState(new State_Loading(m_Play));
                            }
                        }
                    }
            for(int k = 0; k< 3; k++){
                if(m_RectButton[k].Intersects(new Point(x,y))){
                        m_IDChoosed1 = k;
                        if(m_IDChoosed1 == 1){
                            RSAudio.getRS().PlayExplosion();
                            m_Play.setNextState(new State_Menu(m_Play));
                        }
                        else if(m_IDChoosed1 == 0){
                            RSAudio.getRS().PlayBubble();
                            if((m_IDChoosed != 0) && (m_IDChoosed != 1) && (m_IDChoosed != 2)){
                                m_IDChoosed -= 3;
                                if(m_Mask>=0 && m_Mask <=2){
                                    if(m_IDChoosed > -1){
                                        for(int i=0; i<3; i++)
                                            m_CurPos[m_IDChoosed - (m_IDChoosed%3) + i] = new Point(20 + (i%3)*70,-45);
                                        m_Status = 1;
                                        m_NextPos = new Point(m_CurPos[0].X, m_CurPos[0].Y + 70);
                                    }
                                }
                                else if(m_Mask>=3 && m_Mask <=5){
                                    if(m_IDChoosed > 2){
                                        for(int i=0; i<3; i++)
                                            m_CurPos[m_IDChoosed - (m_IDChoosed%3) - 3 + i] = new Point(20 + (i%3)*70,-45);
                                        m_Status = 1;
                                        m_NextPos = new Point(m_CurPos[0].X, m_CurPos[0].Y + 70);
                                    }else{
                                        m_Mask -= 3;
                                    }
                                }
                                else if(m_Mask>=6 && m_Mask <=9){
                                    if(m_IDChoosed > 5){
                                        for(int i=0; i<3; i++)
                                            m_CurPos[m_IDChoosed - (m_IDChoosed%3) - 6 + i] = new Point(20 + (i%3)*70,-45);
                                        m_Status = 1;
                                        m_NextPos = new Point(m_CurPos[0].X, m_CurPos[0].Y + 70);
                                    }else{
                                        m_Mask -= 3;
                                    }
                                }
                           }
                        }
                        else if(m_IDChoosed1 == 2){
                            RSAudio.getRS().PlayBubble();
                            if((m_IDChoosed != m_Level - 1) && (m_IDChoosed != m_Level - 2) && (m_IDChoosed != m_Level - 3)){
                                    m_IDChoosed += 3;
                                    if(m_Mask>=0 && m_Mask <=2){
                                        if(m_IDChoosed < m_Level - 6){
                                            for(int i=0; i<3; i++)
                                                m_CurPos[m_IDChoosed - (m_IDChoosed%3) + 6 + i] = new Point(20 + (i%3)*70, 235);
                                            m_Status = 2;
                                            m_NextPos = new Point(m_CurPos[0].X, m_CurPos[0].Y - 70);
                                        }
                                        else{
                                            m_Mask += 3;
                                        }
                                    }
                                    else if(m_Mask>=3 && m_Mask<=5){
                                        if(m_IDChoosed < m_Level - 3){
                                            for(int i=0; i<3; i++)
                                                m_CurPos[m_IDChoosed - (m_IDChoosed%3) + 3 + i] = new Point(20 + (i%3)*70, 235);
                                            m_Status = 2;
                                            m_NextPos = new Point(m_CurPos[0].X, m_CurPos[0].Y - 70);
                                        }
                                        else{
                                            m_Mask += 3;
                                        }
                                    }
                                    else if(m_Mask>=6 && m_Mask<=9){
                                        if(m_IDChoosed < m_Level){
                                            for(int i=0; i<3; i++)
                                                m_CurPos[m_IDChoosed - (m_IDChoosed%3) + i] = new Point(20 + (i%3)*70, 235);
                                            m_Status = 2;
                                            m_NextPos = new Point(m_CurPos[0].X, m_CurPos[0].Y - 70);
                                        }
                                    }
                            }
                        }
                }
            }
        } 
    }
     public void KeyPressed (int _Keycode){
        if(m_Status == 0){
           if (_Keycode == -1 || _Keycode == 50){
               m_IDChoosed1 = 0;
               RSAudio.getRS().PlayBubble();
                   if((m_IDChoosed != 0) && (m_IDChoosed != 1) && (m_IDChoosed != 2)){
                        m_IDChoosed -= 3;
                        if(m_Mask>=0 && m_Mask <=2){
                            if(m_IDChoosed > -1){
                                for(int i=0; i<3; i++)
                                    m_CurPos[m_IDChoosed - (m_IDChoosed%3) + i] = new Point(20 + (i%3)*70,-45);
                                m_Status = 1;
                                m_NextPos = new Point(m_CurPos[0].X, m_CurPos[0].Y + 70);
                            }
                        }
                        else if(m_Mask>=3 && m_Mask <=5){
                            if(m_IDChoosed > 2){
                                for(int i=0; i<3; i++)
                                    m_CurPos[m_IDChoosed - (m_IDChoosed%3) - 3 + i] = new Point(20 + (i%3)*70,-45);
                                m_Status = 1;
                                m_NextPos = new Point(m_CurPos[0].X, m_CurPos[0].Y + 70);
                            }else{
                                m_Mask -= 3;
                            }
                        }
                        else if(m_Mask>=6 && m_Mask <=9){
                            if(m_IDChoosed > 5){
                                for(int i=0; i<3; i++)
                                    m_CurPos[m_IDChoosed - (m_IDChoosed%3) - 6 + i] = new Point(20 + (i%3)*70,-45);
                                m_Status = 1;
                                m_NextPos = new Point(m_CurPos[0].X, m_CurPos[0].Y + 70);
                            }else{
                                m_Mask -= 3;
                            }
                        }
                   }
           }
            if (_Keycode == -2 || _Keycode == 56){
                m_IDChoosed1 = 2;
                RSAudio.getRS().PlayBubble();
                    if((m_IDChoosed != m_Level - 1) && (m_IDChoosed != m_Level - 2) && (m_IDChoosed != m_Level - 3)){
                            m_IDChoosed += 3;
                            if(m_Mask>=0 && m_Mask <=2){
                                if(m_IDChoosed < m_Level - 6){
                                    for(int i=0; i<3; i++)
                                        m_CurPos[m_IDChoosed - (m_IDChoosed%3) + 6 + i] = new Point(20 + (i%3)*70, 235);
                                    m_Status = 2;
                                    m_NextPos = new Point(m_CurPos[0].X, m_CurPos[0].Y - 70);
                                }
                                else{
                                    m_Mask += 3;
                                }
                            }
                            else if(m_Mask>=3 && m_Mask<=5){
                                if(m_IDChoosed < m_Level - 3){
                                    for(int i=0; i<3; i++)
                                        m_CurPos[m_IDChoosed - (m_IDChoosed%3) + 3 + i] = new Point(20 + (i%3)*70, 235);
                                    m_Status = 2;
                                    m_NextPos = new Point(m_CurPos[0].X, m_CurPos[0].Y - 70);
                                }
                                else{
                                    m_Mask += 3;
                                }
                            }
                            else if(m_Mask>=6 && m_Mask<=9){
                                if(m_IDChoosed < m_Level){
                                    for(int i=0; i<3; i++)
                                        m_CurPos[m_IDChoosed - (m_IDChoosed%3) + i] = new Point(20 + (i%3)*70, 235);
                                    m_Status = 2;
                                    m_NextPos = new Point(m_CurPos[0].X, m_CurPos[0].Y - 70);
                                }
                            }
                    }
            }
            if (_Keycode == -3 || _Keycode == 52){
                RSAudio.getRS().PlayBubble();
                if(m_Mask == 0){
                    if(m_IDChoosed != 0){
                        m_IDChoosed -= 1;
                        for(int i=0; i<3; i++)
                            m_CurPos[m_IDChoosed - (m_IDChoosed%3) + i] = new Point(20 + (i%3)*70,-45);
                        m_Mask = 2;
                        m_Status = 1;
                        m_NextPos = new Point(m_CurPos[0].X, m_CurPos[0].Y + 70);
                    }
                }
                else{
                    if(m_IDChoosed != 0){
                        m_IDChoosed -= 1;
                        m_Mask -= 1;
                    }
                }
            }
            if (_Keycode == -4 || _Keycode == 54){
                RSAudio.getRS().PlayBubble();
                if(m_Mask == 8){
                    if(m_IDChoosed != m_Level - 1){
                        m_IDChoosed += 1;
                        for(int i=0; i<3; i++)
                            m_CurPos[m_IDChoosed - (m_IDChoosed%3) + i] = new Point(20 + (i%3)*70,235);
                        m_Mask = 6;
                        m_Status = 2;
                        m_NextPos = new Point(m_CurPos[0].X, m_CurPos[0].Y - 70);
                    }
                }
                else{
                    if(m_IDChoosed != m_Level - 1){
                        m_IDChoosed += 1;
                        m_Mask += 1;
                    }
                }
            }
            if (_Keycode == -5 || _Keycode == 53){
                RSAudio.getRS().PlayExplosion();
                if(m_Lock[m_IDChoosed]==1){
                    RSAudio.getRS().StopBack();
                    Level.get().setCurLevel(m_IDChoosed + 1);
                    m_Play.setNextState(new State_Loading(m_Play));
                }
            }
            
            if(_Keycode == 48){
                RSAudio.getRS().PlayExplosion();
                m_IDChoosed1 = 1;
                m_Play.setNextState(new State_Menu(m_Play));
            }
      }
    }
    public void Render (){
        super.Render();
        m_Graphics.setColor(0, 0, 0);
        m_Graphics.fillRect(0, 0, 240, 320);
        
        m_Home.setFrame(0);
        m_Home.setPosition(99, 244);
        m_Home.paint(m_Graphics);
        
        m_Point.setFrame(0);
        m_Point.setPosition(73, 212);
        m_Point.setTransform(Sprite.TRANS_ROT90);
        m_Point.paint(m_Graphics);
        
        m_Point.setFrame(0);
        m_Point.setPosition(119, 283);
        m_Point.setTransform(Sprite.TRANS_ROT270);
        m_Point.paint(m_Graphics);
        
        for(int i=0; i<3 ; i++){
            if(m_IDChoosed1==i){
                 m_Ball.setFrame(1);
                 m_Ball.setPosition(20 + 70*i, 235);
                 m_Ball.paint(m_Graphics);
            }else {
                 m_Ball.setFrame(0);
                 m_Ball.setPosition(20 + 70*i, 235);
                 m_Ball.paint(m_Graphics);
            }
        }
        if(m_IDChoosed1 != 1){
            m_FPS ++;
            if(m_FPS >=15){
                m_FPS = 0;
                m_IDChoosed1 = -1;
            }
        }
        for(int i = 0; i<m_Level; i++){
            if(m_Rect[i].Intersects(m_Screen)&&(m_Rect[i].Intersects(m_RectButton[0])==false)
                    &&(m_Rect[i].Intersects(m_RectButton[1])==false)
                    &&(m_Rect[i].Intersects(m_RectButton[2])==false)){
               if(i==m_IDChoosed){
                   m_Ball.setFrame(1);
                   m_Ball.setPosition(m_CurPos[i].X, m_CurPos[i].Y);
                   m_Ball.paint(m_Graphics);
               }
               else{
                   m_Ball.setFrame(0);
                   m_Ball.setPosition(m_CurPos[i].X, m_CurPos[i].Y);
                   m_Ball.paint(m_Graphics);
               }
               if(m_Lock[i]==0){
                   m_Lock1.setFrame(0);
                   m_Lock1.setPosition(m_CurPos[i].X +10, m_CurPos[i].Y + 7);
                   m_Lock1.paint(m_Graphics);
               }
               if(i+1>=10)
                   RSFont.getRS().getFont(Global.FONT_BIG).DrawString(
                            "" + (i+1),m_CurPos[i].X + 23,m_CurPos[i].Y + 21, m_Graphics);
               else
                   RSFont.getRS().getFont(Global.FONT_BIG).DrawString(
                            "" + (i+1),m_CurPos[i].X + 26,m_CurPos[i].Y + 21, m_Graphics);
           } 
        }
    }
    public void Destroy (){
        m_Home = null;           
        m_Point = null; 
        m_Ball = null;    
        m_Lock1 = null;
    }
}

